package com.ruoyi.common.utils.poi;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

public class Demo {

    public static void main(String[] args) {
        String[] imagePaths = {"C:\\Users\\cc517\\Desktop\\统一加油费\\微信图片_20251016193254.jpg", "C:\\Users\\cc517\\Desktop\\统一加油费\\微信图片_20251016193332.jpg", "C:\\Users\\cc517\\Desktop\\统一加油费\\微信图片_20251016193254.jpg"}; // 图片路径

        try {
            // 第一步：读取所有图片，计算总宽度和最大高度
            BufferedImage[] images = new BufferedImage[imagePaths.length];
            int totalWidth = 0;
            int maxHeight = 0;

            for (int i = 0; i < imagePaths.length; i++) {
                BufferedImage img = ImageIO.read(new File(imagePaths[i]));
                if (img != null) {
                    images[i] = img;
                    totalWidth += img.getWidth();
                    maxHeight = Math.max(maxHeight, img.getHeight());
                } else {
                    System.err.println("无法读取图片: " + imagePaths[i]);
                }
            }

            // 第二步：创建结果图像，宽度为总宽度，高度为最大高度
            BufferedImage resultImage = new BufferedImage(totalWidth, maxHeight, BufferedImage.TYPE_INT_ARGB);
            Graphics2D g2d = resultImage.createGraphics();

            // 第三步：逐个绘制图片
            int x = 0;
            for (BufferedImage img : images) {
                if (img != null) {
                    g2d.drawImage(img, x, 0, null);
                    x += img.getWidth();
                }
            }

            g2d.dispose();

            // 第四步：保存结果图像
            ImageIO.write(resultImage, "PNG", new File("result.png"));
            System.out.println("图片拼接完成，已保存为 result.png");

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
